package com.lovo.atm.dao.impl;

import com.lovo.atm.bean.AtmUserBean;
import com.lovo.atm.dao.IAtmDao;
import com.lovo.homework.bean.UserBean;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class IAtmDaoImpl extends BaseDao implements IAtmDao {

    private List<AtmUserBean> list = new ArrayList<>();

    private AtmUserBean userBean = null;

    @Override
    public AtmUserBean Login(String name, String psw) {
        this.setCon();
        try {
            ps = con.prepareStatement("select * from t_atmUser where pk_atmUserId=? and a_psw=? ");
            ps.setObject(1, name);
            ps.setObject(2, psw);
            rs = ps.executeQuery();
            if (rs.next()) {
                userBean = cv();
                return userBean;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return null;
    }
    @Override
    public AtmUserBean findByName(String code) {
        this.setCon();
        try {
            ps = con.prepareStatement("select * from t_atmUser where pk_atmUserId=? ");
            ps.setObject(1, code);
            rs = ps.executeQuery();
            if (rs.next()) {
                userBean = cv();
                return userBean;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    @Override
    public void update(String code, double money) {
        this.setCon();
        try {
            ps = con.prepareStatement("update t_atmUser set a_count=a_count+? where pk_atmUserId=? ");
            ps.setObject(1, money);
            ps.setObject(2, code);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public AtmUserBean cv() throws SQLException {
        AtmUserBean user = new AtmUserBean();
        user.setName(rs.getString("pk_atmUserId"));
        user.setCount(rs.getInt("a_count"));
        user.setPsw(rs.getString("a_psw"));
        return user;
    }
}
